package me.wangqu.weiyouqu.web.rest.business.dao.impl;

import me.wangqu.weiyouqu.web.rest.business.dao.UserDao;
import me.wangqu.weiyouqu.web.rest.business.model.UserModel;
import me.wangqu.weiyouqu.web.rest.util.DateUtil;
import me.wangqu.weiyouqu.web.rest.util.constant.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by arcticfox on 6/22/16.
 */
@Repository
public class UserDaoImpl implements UserDao {

    private final Logger log = LoggerFactory.getLogger(UserDaoImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public boolean createUser(final UserModel model) {
        log.debug(">>>>>>>> 创建 WEB 用户");
        if (!ObjectUtils.isEmpty(model)) {
            StringBuffer sql = new StringBuffer(512);
            List<Object> params = new ArrayList<Object>();
            sql.append("INSERT INTO userdata");
            sql.append("  (tj_id,");
            sql.append("   phone,");
            sql.append("   pass,");
            sql.append("   money,");
            sql.append("   income,");
            sql.append("   expenditure,");
            sql.append("   wx,");
            sql.append("   realname,");
            sql.append("   alipay,");
            sql.append("   wgateid,");
            sql.append("   ip,");
            sql.append("   kou,");
            sql.append("   day,");
            sql.append("   time)");
            sql.append("VALUES");
            sql.append("  (?, ?, ?, ?, ?, ?, '', '', '', '', ?, ?, ?, ?)");
            params.add(model.getPid() == null ? 0 : model.getPid());
            params.add(model.getLogin());
            params.add(model.getPassword());
            params.add(Constant.REGISTER_SONG);
            params.add(Constant.REGISTER_SONG);
            params.add(BigDecimal.ZERO);
            params.add(model.getIp());
            params.add(100);
            params.add(DateUtil.getCurrentDateString());
            params.add(DateUtil.getCurrentUnixTime());
            return this.jdbcTemplate.update(sql.toString(), params.toArray()) > 0;
        }
        return false;
    }

    @Override
    public boolean changePassword(UserModel model) {
        log.debug(">>>>>>>> 更新 WEB 用户密码");
        if (!ObjectUtils.isEmpty(model)) {
            StringBuffer sql = new StringBuffer("update userdata u set u.pass = ? where u.phone = ?");
            List<String> params = new ArrayList<String>();
            params.add(model.getPassword());
            params.add(model.getLogin());
            return jdbcTemplate.update(sql.toString(), params.toArray()) > 0;
        }
        return false;
    }

    @Override
    public UserModel findByLogin(String login) {
        log.debug(">>>>>>>> 查询 WEB 用户详情");
        if (!ObjectUtils.isEmpty(login)) {
            StringBuffer sql = new StringBuffer(512);
            List<String> params = new ArrayList<String>();
            sql.append("SELECT id as id, tj_id as pid");
            sql.append("  FROM userdata");
            sql.append(" WHERE phone = ? limit 1");
            params.add(login);
            return jdbcTemplate.queryForObject(sql.toString(), params.toArray(), new RowMapper<UserModel>() {
                @Override
                public UserModel mapRow(ResultSet rs, int i) throws SQLException {
                    UserModel model = new UserModel();
                    model.setId(rs.getLong("id"));
                    model.setPid(rs.getLong("pid"));
                    return model;
                }
            });
        }
        return null;
    }
}
